﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Xiaozilang_Ordering_System
{
    public partial class AddImage : Form
    {
        //搭建浏览本机对象
        OpenFileDialog file = new OpenFileDialog();
        bool red = false;
        string sql;
        public AddImage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            switch (GeneralVariable.count1)
            {
                //添加菜品
                case 1:
                    Add_dishes();
                    break;
                //修改商家信息
                case 2:
                    Modify_the_business_picture();
                    break;
                //修改用户信息
                case 3:
                    Modify_user_profile();
                    break;
                //删除菜品
                case 4:
                    Delete_dishes();
                    break;
                //修改菜品
                case 5:
                    Modify_dish_information();
                    break;
            }
            if (GeneralVariable.count1 == 2 || GeneralVariable.count1 == 3 || GeneralVariable.count1 == 5)
            {
                if (red == true)
                {

                    MessageBox.Show("修改成功");

                    //刷新界面
                    this.Close();
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //要打开的路径
            string a = @"D:\Work\git\shankouzu\项目编码\Xiaozilang_Ordering_System\bin\Debug\imag\".ToString();
            Browse_local_picture_port(pictureBox1, a);
        }
        private void AddDish_Load(object sender, EventArgs e)
        {
            //文字背景设为透明
            label1.BackColor = Color.Transparent;
            label2.BackColor = Color.Transparent;
            switch (GeneralVariable.count1) 
            {
                case 1:
                    //添加菜品界面
                    Add_dishes_interface();
                    break;
                case 2:
                    //商家信息界面
                    Modify_merchant_information();
                    break;
                case 3:
                    //用户信息界面
                    Modify_user_information();
                    break;
                case 4:
                    //删除菜品界面
                    Delete_menu_interface();
                    break;
                case 5:
                    //修改菜品信息界面
                    Modify_menu_information_interface();
                    break;
            }
        }
        /*修改菜品界面
         参数：无
         返回值：无*/
        private void Modify_menu_information_interface()
        {
            Text = "修改菜品";
            Business.Pirbox(GeneralVariable.dishimage,pictureBox1);
            button2.Text = "选择图片";
            textBox1.Text = GeneralVariable.dishName;
            textBox2.Text = GeneralVariable.Price;
        }
        /*删除菜品
         参数：无
         返回值：无*/
        private void Delete_menu_interface ()
        {
            Text = "删除菜品";
            Business.Pirbox(GeneralVariable.dishimage, pictureBox1);
            textBox1.Text = GeneralVariable.dishName;
            textBox2.Text = GeneralVariable.Price;
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            button2.Visible = false;
        }
        /*商家信息界面
         参数：无
         返回值：无*/
        private void Modify_merchant_information() 
        {
            label1.Text = "店名：";
            label2.Text = "地址：";
            Text = "修改商家头像";
            button2.Text = "上传";
            textBox1.Text = denglu.Name1;
            textBox2.Text = denglu.address;
            Business.Pirbox(denglu.imagelj,pictureBox1);
        }
        /*用户信息界面
         * 参数：无
         * 返回值：无
         */
        private void Modify_user_information()
        {
            label1.Text = "昵称：";
            label2.Text = "地址：";
            textBox1.Text = denglu.Name1;
            textBox2.Text = denglu.address;
            button2.Text = "上传";
            Business.Pirbox(denglu.imagelj, pictureBox1);
        }
        /*添加菜品
         * 参数：无
         * 返回值：无
         */
        private void Add_dishes_interface()
        {
            Text = "添加菜品";
        }
        /*预览本机图片并显示
         参数：
         返回值：无*/
        private void Browse_local_picture_port(PictureBox pictureBox, string route)
        {
            try
            {
                if (!Directory.Exists(@"" + route + ""))
                {
                    //创建路径
                    Directory.CreateDirectory(@"" + route + "");
                }
                //设置默认打开路径(绝对路径)
                file.InitialDirectory = @"" + route + "";
                file.ShowDialog();
                string path = @"" + route + "" + file.SafeFileName;
                if (file.SafeFileName != "")
                {
                    Business.Pirbox(path,pictureBox1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("404{0}", ex.Message);
            }
        }
        /*修改菜品
         *参数：无
         *返回值：无
         */
        private void Modify_dish_information()
        {
            try
            {
                if (file.SafeFileName == "")
                {
                    sql = string.Format("update Dish set dName='{0}',unitPrice='{1}',image='{2}' where dishID='{3}'", textBox1.Text, textBox2.Text, GeneralVariable.dishimage, GeneralVariable.DishID);
                }
                else
                {
                    sql = string.Format("update Dish set dName='{0}',unitPrice='{1}',image='imag/{2}' where dishID='{3}'", textBox1.Text, textBox2.Text, file.SafeFileName, GeneralVariable.DishID);
                }
                red = DBHelper.GetNonQuery(sql);
            }
            catch (Exception ex)
            {
                MessageBox.Show("未知错误{0}", ex.Message);
            }
        }
        /*删除菜品
         * 参数：无
         * 返回值：无
         */
        private void Delete_dishes() 
        {
            try
            {
                sql = string.Format("update Dish set condition='否' where dishID='{0}'", GeneralVariable.DishID);
                red = DBHelper.GetNonQuery(sql);
                MessageBox.Show("删除成功");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("未知错误{0}", ex.Message);
            }
        }
        /*修改商家信息
         * 参数：无
         * 返回值：无
         */
        private void Modify_the_business_picture() 
        {
            try
            {
                if (file.SafeFileName == "")
                {
                    sql = string.Format("update Business set BSimage='{0}',BSaddress='{1}',BSname='{2}' where BSID='{3}'",denglu.imagelj, textBox2.Text, textBox1.Text, denglu.ID);
                }
                else
                {
                    sql = string.Format("update Business set BSimage='imag/{0}',BSaddress='{1}',BSname='{2}' where BSID='{1}'", file.SafeFileName, textBox2.Text, textBox1.Text, denglu.ID);
                    denglu.imagelj = "imag/" + file.SafeFileName;
                }
                denglu.Name1 = textBox1.Text;
                denglu.address = textBox2.Text;
                red = DBHelper.GetNonQuery(sql);

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("未知错误{0}", ex.Message);
            }
        } 
        /*修改用户信息
         * 参数：无
         * 返回值：无
         */
        private void Modify_user_profile() 
        {
            try
            {
                if (file.SafeFileName == "")
                {
                    sql = string.Format("update User1 set USimage='{0}',USname='{1}',USaddress='{2}' where UserID='{3}'", denglu.imagelj,textBox1.Text,textBox2.Text,denglu.ID);
                }
                else
                {
                    sql = string.Format("update User1 set USimage='imag/{0}' where UserID='{1}'", file.SafeFileName,Shopping_Cart.UserJudge, textBox2.Text,denglu.ID);
                    denglu.imagelj = "imag/" + file.SafeFileName;
                }
                red = DBHelper.GetNonQuery(sql);
                denglu.Name1 = textBox1.Text;
                denglu.address = textBox2.Text;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("未知错误{0}", ex.Message);
            }
        }
        /*添加菜品
         * 参数：无
         * 返回值：无
         */
        private void Add_dishes() 
        {
            if (textBox1.Text == "" || textBox2.Text == "" || file.SafeFileName == "")
            {
                MessageBox.Show("请输入相关信息");
            }
            else
            {
                try
                {
                    sql = string.Format("insert into Dish(dName,unitPrice,BSID,image,condition) values('{0}','{1}','{2}','imag/{3}','是')", textBox1.Text, textBox2.Text, denglu.ID, file.SafeFileName);
                    red = DBHelper.GetNonQuery(sql);
                    if (red == true)
                    {
                        MessageBox.Show("添加成功");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("404{0}", ex.Message);
                }
            }
        }
    }
}
